#define  _CRT_SECURE_NO_WARNINGS
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        if (root == nullptr) return 0;
        deque<pair<TreeNode*, size_t>> dq;
        dq.push_back({ root, 0 });
        size_t ret = 1;
        while (dq.size())
        {
            int size = dq.size();
            ret = max(ret, dq.back().second - dq[0].second + 1);
            while (size--)
            {
                TreeNode* cur = dq.front().first;
                size_t index = dq.front().second;
                // auto& [cur, index] = dq[0];
                dq.pop_front();
                if (cur->left) dq.push_back({ cur->left, index * 2 + 1 });
                if (cur->right) dq.push_back({ cur->right, index * 2 + 2 });
            }
        }
        return ret;
    }
};